<?php
namespace App\Http\Controllers\Member;

use App\Models\UserCollect;
use Illuminate\Support\Facades\Input;
use App\Models\CompanyTags;
use Illuminate\Http\Request;
use App\Models\Employ;
use App\Models\Teaches;
use App\Models\CompanyJobs;
use App\Models\Company;
use App\Models\BbsQuestion;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\EmployExt;
use App\Models\BbsQuestionFocus;
use App\Models\TeachesContent;
use App\Models\TeachesCompany;
/**
 *
 * @author Joker
 *        
 */
class CollectController extends MemberController
{
    
     /* (non-PHPdoc)
      * @see \App\Http\Controllers\Member\MemberController::__construct()
      */
     public function __construct() {
         parent::__construct();
         $this->gzxz = array(0=>'全职',1=>'兼职',2=>'实习',100=>'不限');
         $this->salary = array(0=>'2k以下',1=>'2k~5k',2=>'5k~10k',3=>'10k~15k',4=>'15k~25k',5=>'25k~50k',6=>'50k以上',100=>'不限');
         $this->gznx = array(0=>'应届毕业生',1=>'1年',2=>'2年',3=>'3年',4=>'4年',5=>'5年',6=>'6年',7=>'7年',8=>'8年',9=>'9年',10=>'10年',11=>'10年以上',100=>'不限');
         $this->degree = array(0=>'大专',1=>'本科',2=>'硕士',3=>'博士',4=>'其他',100=>'不限');
     }

    /**
     * 就业指南的收藏
     */
    public function index()
    {
        $col = new UserCollect();
        $collects = $col->getList('employ', $this->uid);
        return view('member.collect.employ')->with('documents',$collects);
    }
    
    /**
     * 用户收藏
     */
    public function store(Request $req)
    {
        $data = $req->only('app','appid');
        $result = collect();
        switch ($data['app'])
        {
            case 'employ':
                $result = Employ::find($data['appid']);
                break;
            case 'teaches':
                $result = Teaches::find($data['appid']);
                break;
            case 'teaches_content':
                $result = TeachesContent::find($data['appid']);
                break;
            case 'teaches_company':
                $result = TeachesCompany::find($data['appid']);
                break;
            case 'jobs':
                $result = CompanyJobs::find($data['appid']);
                break;
            case 'company':
                $result = Company::find($data['appid']);
                break;
            case 'bbs':
                $result = BbsQuestion::find($data['appid']);
                $focus = new BbsQuestionFocus();
                $focus->question_id = $result->id;
                $focus->user_id = $this->uid;
                $focus->created_at = date("Y-m-d H:i:s");
                $focus->save();
                break;
        }
        if(empty($result))
        {
            return  $this->formatJson('falied','抱歉，您要收藏的资源不存在!');
        }
        else
        {
           $collect = UserCollect::where(array('app'=>$data['app'],'app_id'=>$data['appid'],'user_id'=>$this->uid))->first();     
           if(empty($collect))
           {
               $tocoll = new UserCollect();
               $tocoll->app = $data['app'];
               $tocoll->app_id = $data['appid'];
               $tocoll->user_id =$this->uid;
               DB::beginTransaction();
               try{
                   $tocoll->save();
                   switch ($data['app'])
                   {
                       case 'employ': //就业指南文章收藏
                           $ext = EmployExt::find($data['appid']);
                           $ext->incrementAdd('recommond_num',1);
                           break;
                       case 'teaches':// 充电加油课程收藏
                           $result->incrementAdd('attention_num',1);
                           break;
                       case 'teaches_content': // 充电加油 教程收藏
                           $result->incrementAdd('attention_num',1);
                           break;
                       case 'teaches_company'://企业内训课程收藏
                           $result->incrementAdd('attention_num',1);
                           break;
                       case 'bbs':// 论坛帖子收藏
                           $result->incrementAdd('focus_count',1);
                           break;
                   }
                   DB::commit();
               }catch (\PDOException $e){
                   Log::info('用户收藏失败：'.$e->getMessage());
                   DB::rollBack();
               }
               return  $this->formatJson('success','恭喜您，收藏成功！');
           }
           else 
           {
               return  $this->formatJson('falied','抱歉，您已收藏该资源!');
           }
        }
    }
    
    /**
     * 我的收藏  充电加油 课程
     */
    public function teaches()
    {
        $col = new UserCollect();
        $collects = $col->getList('teaches', $this->uid);
        return view('member.collect.teaches')->with('documents',$collects);
    }
    
    /**
     * 公司收藏
     * @return \Illuminate\View\$this
     */
    public function company()
    {
        $col = new UserCollect();
        $companys = $col->companyList($this->uid,'company');
        return view('member.collect.companys')->with('documents',$companys);
    }
    
    /**
     * 职位收藏
     */
    public function jobs()
    {
        $col = new UserCollect();
        $companys = $col->jobsList($this->uid,'jobs');
        foreach ($companys as $company)
        {
            $company->tags = CompanyTags::with('tags')->where('company_id',$company->company_id)->skip(0)->take(2)->get();
        }
        return view('member.collect.jobs')->with('collect_job',$companys)->with('gzxz',$this->gzxz)
                ->with('gznx',$this->gznx)->with('salary',$this->salary)->with('degree',$this->degree);
    }
    
    /**
     * 帖子收藏
     */
    public function bbs()
    {
         $col = new UserCollect();
         $question = $col->bbsList($this->uid, 'bbs');
         return view('member.bbs.collect')->with('question',$question);
    }
    
    public function destroy($id)
    {
        $app=Input::get('app','');
        $collect = UserCollect::where(array('app'=>$app,'id'=>$id,'user_id'=>$this->uid))->first();
        if(empty($collect))
        {
            return  $this->formatJson('falied','抱歉，您要取消收藏的资源不存在!');
        }
        else
        {
           DB::beginTransaction();
           try {
                if($app == 'bbs'){
                    $result = BbsQuestion::find($collect->app_id);
                    $result->decrementDel('focus_count',1);
                }elseif ($app == 'employ'){
                    $result = EmployExt::find($collect->app_id);
                    $result->decrementDel('recommond_num',1);
                }elseif ($app == 'teaches'){
                    $result = Teaches::find($collect->app_id);
                    $result->decrementDel('attention_num',1);
                }elseif ($app == 'teaches_content'){
                    $result = TeachesContent::find($collect->app_id);
                    $result->decrementDel('attention_num',1);
                }elseif ($app == 'teaches_company'){
                    $result = TeachesCompany::find($collect->app_id);
                    $result->decrementDel('attention_num',1);
                }
                $collect->forceDelete();
                DB::commit();
           }catch (\PDOException $e){
               Log::info(['取消收藏异常'=>$e->getMessage()]);
               DB::rollBack();
           }
            return $this->formatJson('success','取消收藏成功！');
        }
    }
}